<?php

namespace app\api\controller;
use Flc\Dysms\Client;
use Flc\Dysms\Request\SendSms;
use app\common\controller\DBase;



class SendMessage
{


    /**
     * 发送验证码消息
     * @desc 用户登录发送验证码
     * @author xx
     * @date  2024-07-01测试
     */
    public function sendCode($params=[])
    {
      try{
        $tel             = $params['tel']??'';
        $smsType         = $params['smsType']??'1';
        $data['tel']     = trim($tel);
        $data['smsType'] = $smsType;
        $randomCode       = rand(100000, 999999);
        $data['tel']      = $data['tel'];
        $data['code']     = $randomCode;
        $data['sms_type'] = $data['smsType'];
        //获取短信模板ID
        $smsTemplateId     = env('MESS_MESS_LOGIN');
        $signature         = env('MESS_MESS_SIGNATURE');
        $config = [
            'accessKeyId'     => env('MESS_MESS_ACCESS_ID'),
            'accessKeySecret' => env('MESS_MESS_ACCESS_SECRET')
        ];
        $client  = new Client($config);
        $sendSms = new SendSms();
        $sendSms->setPhoneNumbers($data['tel']);
        $sendSms->setSignName($signature);
        $sendSms->setTemplateCode($smsTemplateId);
        $sendSms->setTemplateParam(['code'=>$randomCode]);
        $sendSms->setOutId(getVcode(6));
        $res = $client->execute($sendSms);
        $res = objectToArray($res);
        $data['message'] = $res['Message']??'';
          return  ['res'=>$res,'data'=>$data];
        }catch (\Exception $e) {
          return json(DefaultResponse(201, false, $e->getMessage(), null));
        }
    }

}